<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Node.js,Pomelo,Unity3D," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="系统版本：macOS 10.12.3，Pomelo版本：1.2.3，Unity版本：5.4.4f1
Pomelo Demo 服务端代码：https://github.com/vitahlin/VitahPomeloServer
Pomelo Demo 客户端代码：https://github.com/vitahlin/VitahPomeloClient
关于 Mongoose 的介绍网络上有很多，">
<meta property="og:type" content="article">
<meta property="og:title" content="Pomelo教程(7)-新建用户服务器和用户信息表">
<meta property="og:url" content="http://www.vitah.net/posts/dc1a186b/index.html">
<meta property="og:site_name" content="Vitah's Blog">
<meta property="og:description" content="系统版本：macOS 10.12.3，Pomelo版本：1.2.3，Unity版本：5.4.4f1
Pomelo Demo 服务端代码：https://github.com/vitahlin/VitahPomeloServer
Pomelo Demo 客户端代码：https://github.com/vitahlin/VitahPomeloClient
关于 Mongoose 的介绍网络上有很多，">
<meta property="og:updated_time" content="2017-03-05T10:15:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Pomelo教程(7)-新建用户服务器和用户信息表">
<meta name="twitter:description" content="系统版本：macOS 10.12.3，Pomelo版本：1.2.3，Unity版本：5.4.4f1
Pomelo Demo 服务端代码：https://github.com/vitahlin/VitahPomeloServer
Pomelo Demo 客户端代码：https://github.com/vitahlin/VitahPomeloClient
关于 Mongoose 的介绍网络上有很多，">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://www.vitah.net/posts/dc1a186b/"/>





  <title> Pomelo教程(7)-新建用户服务器和用户信息表 | Vitah's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Vitah's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocapitalize="off" autocomplete="off" autocorrect="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://www.vitah.net/posts/dc1a186b/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Vitah">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/head.JPG">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Vitah's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Pomelo教程(7)-新建用户服务器和用户信息表
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-03-05T18:15:33+08:00">
                2017-03-05
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Pomelo/" itemprop="url" rel="index">
                    <span itemprop="name">Pomelo</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/posts/dc1a186b/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="posts/dc1a186b/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><strong>系统版本：macOS 10.12.3，Pomelo版本：1.2.3，Unity版本：5.4.4f1</strong></p>
<p><strong>Pomelo Demo 服务端代码：<a href="https://github.com/vitahlin/VitahPomeloServer" target="_blank" rel="external">https://github.com/vitahlin/VitahPomeloServer</a></strong></p>
<p><strong>Pomelo Demo 客户端代码：<a href="https://github.com/vitahlin/VitahPomeloClient" target="_blank" rel="external">https://github.com/vitahlin/VitahPomeloClient</a></strong></p>
<p>关于 Mongoose 的介绍网络上有很多，这里不再赘述。需要了解相关概念可以自行百度。</p>
<p>数据库安装完成后，我们需要往数据库里面保存相关数据，一个游戏里面玩家的用户数据是必不可少的，这里我们以此为例，介绍怎么使用 Mongoose 来进行数据的操作和存储。</p>
<h3 id="创建-roleSchema-js"><a href="#创建-roleSchema-js" class="headerlink" title="创建 roleSchema.js"></a>创建 roleSchema.js</h3><p>在 Mongoose 中，所有东西都从一个 Schema 开始。每一个 schema 都映射到一个 MongoDb 的集合，并定义了该集合中的文档的形式。Schema 是一种以文件形式存储的数据库模型骨架，不具备数据库的操作能力，仅仅只是一段代码，无法通往数据库端, 仅仅只是数据库模型在程序片段中的一种表现。</p>
<a id="more"></a>
<p>创建<code>game-server/app/schemas/roleSchems.js</code>用来定义玩家的信息结构：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> mongoose = <span class="built_in">require</span>(<span class="string">'mongoose'</span>);</div><div class="line"></div><div class="line"><span class="comment">///////////////////////////////////////////////////////////</span></div><div class="line"></div><div class="line"><span class="keyword">var</span> roleSchema = <span class="keyword">new</span> mongoose.Schema(&#123;</div><div class="line">	<span class="comment">// 角色id</span></div><div class="line">	uid: &#123;</div><div class="line">		<span class="attr">type</span>: <span class="built_in">Number</span>,</div><div class="line">		<span class="attr">required</span>: <span class="literal">true</span>,</div><div class="line">		<span class="attr">unique</span>: <span class="literal">true</span>,</div><div class="line">		<span class="attr">index</span>: <span class="literal">true</span></div><div class="line">	&#125;,</div><div class="line"></div><div class="line">	<span class="comment">// 玩家等级</span></div><div class="line">	lv: &#123;</div><div class="line">		<span class="attr">type</span>: <span class="built_in">Number</span>,</div><div class="line">		<span class="attr">default</span>: <span class="number">1</span></div><div class="line">	&#125;,</div><div class="line"></div><div class="line">	<span class="comment">// 玩家昵称</span></div><div class="line">	name: &#123;</div><div class="line">		<span class="attr">type</span>: <span class="built_in">String</span>,</div><div class="line">		<span class="attr">default</span>: <span class="string">'testname'</span></div><div class="line">	&#125;,</div><div class="line"></div><div class="line">	<span class="comment">// 金币</span></div><div class="line">	gold: &#123;</div><div class="line">		<span class="attr">type</span>: <span class="built_in">Number</span>,</div><div class="line">		<span class="attr">default</span>: <span class="number">0</span></div><div class="line">	&#125;,</div><div class="line"></div><div class="line">	<span class="comment">// 钻石</span></div><div class="line">	diamond: &#123;</div><div class="line">		<span class="attr">type</span>: <span class="built_in">Number</span>,</div><div class="line">		<span class="attr">default</span>: <span class="number">0</span></div><div class="line">	&#125;</div><div class="line">&#125;);</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 获取用户id</div><div class="line"> * @return &#123;[type]&#125; [description]</div><div class="line"> */</div><div class="line">roleSchema.methods.getUid = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">	<span class="keyword">return</span> <span class="keyword">this</span>.uid;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 获取用户等级</div><div class="line"> * @return &#123;[type]&#125; [description]</div><div class="line"> */</div><div class="line">roleSchema.methods.getLv = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">	<span class="keyword">return</span> <span class="keyword">this</span>.lv;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 获取用户名称</div><div class="line"> * @return &#123;[type]&#125; [description]</div><div class="line"> */</div><div class="line">roleSchema.methods.getName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">	<span class="keyword">return</span> <span class="keyword">this</span>.name;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 设置用户名字</div><div class="line"> * @param &#123;[type]&#125; new_name [新的名字]</div><div class="line"> */</div><div class="line">roleSchema.methods.setName = <span class="function"><span class="keyword">function</span>(<span class="params">new_name</span>) </span>&#123;</div><div class="line">	<span class="keyword">this</span>.name = new_name;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 获取金币</div><div class="line"> * @return &#123;[type]&#125; [description]</div><div class="line"> */</div><div class="line">roleSchema.methods.getGold = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">	<span class="keyword">return</span> <span class="keyword">this</span>.gold;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 添加金币</div><div class="line"> */</div><div class="line">roleSchema.methods.addGold = <span class="function"><span class="keyword">function</span>(<span class="params">add_gold</span>) </span>&#123;</div><div class="line">	add_gold = <span class="built_in">parseInt</span>(add_gold);</div><div class="line">	<span class="keyword">if</span> (add_gold &gt;= <span class="number">0</span>) &#123;</div><div class="line">		<span class="keyword">this</span>.gold += add_gold;</div><div class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	<span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 扣除金币</div><div class="line"> * @return &#123;[type]&#125; [description]</div><div class="line"> */</div><div class="line">roleSchema.methods.subGold = <span class="function"><span class="keyword">function</span>(<span class="params">sub_gold</span>) </span>&#123;</div><div class="line">	sub_gold = <span class="built_in">parseInt</span>(sub_gold);</div><div class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.gold &lt; sub_gold) &#123;</div><div class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">this</span>.gold -= sub_gold;</div><div class="line">	<span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 获取用户钻石</div><div class="line"> * @return &#123;[type]&#125; [description]</div><div class="line"> */</div><div class="line">roleSchema.methods.getDiamond = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">	<span class="keyword">return</span> <span class="keyword">this</span>.diamond;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 添加钻石</div><div class="line"> */</div><div class="line">roleSchema.methods.addDiamond = <span class="function"><span class="keyword">function</span>(<span class="params">add_diamond</span>) </span>&#123;</div><div class="line">	add_diamond = <span class="built_in">parseInt</span>(add_diamond);</div><div class="line">	<span class="keyword">if</span> (add_diamond &gt;= <span class="number">0</span>) &#123;</div><div class="line">		<span class="keyword">this</span>.diamond += add_diamond;</div><div class="line">		<span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 扣除钻石</div><div class="line"> * @return &#123;[type]&#125; [description]</div><div class="line"> */</div><div class="line">roleSchema.methods.subDiamond = <span class="function"><span class="keyword">function</span>(<span class="params">sub_diamond</span>) </span>&#123;</div><div class="line">	sub_diamond = <span class="built_in">parseInt</span>(sub_diamond);</div><div class="line">	<span class="keyword">if</span> (<span class="keyword">this</span>.diamond &lt; sub_diamond) &#123;</div><div class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">this</span>.diamond -= sub_diamond;</div><div class="line">	<span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">// 生成json格式内容的函数</span></div><div class="line"><span class="keyword">if</span> (!roleSchema.options.toJSON) &#123;</div><div class="line">	roleSchema.options.toJSON = &#123;&#125;;</div><div class="line">&#125;</div><div class="line"><span class="comment">/* jshint unused:false */</span></div><div class="line">roleSchema.options.toJSON.transform = <span class="function"><span class="keyword">function</span>(<span class="params">doc, ret</span>) </span>&#123;</div><div class="line">	<span class="keyword">delete</span> ret._id;</div><div class="line">	<span class="keyword">delete</span> ret.__v;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">// 生成Role模型</span></div><div class="line">mongoose.model(<span class="string">'Role'</span>, roleSchema);</div></pre></td></tr></table></figure></p>
<p>我们可以直接修改<code>roleSchema</code>文件来修改字段、类型、默认值等等，也可以自定义需要的函数来实现对字段的操作。</p>
<h3 id="创建-roleModel-js"><a href="#创建-roleModel-js" class="headerlink" title="创建 roleModel.js"></a>创建 roleModel.js</h3><p>接着创建<code>game-server/app/models/roleModel.js</code>用来实现增删改查等操作：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> roleSchema = <span class="built_in">require</span>(<span class="string">'../schemas/roleSchema'</span>);</div><div class="line"><span class="keyword">var</span> mongoose = <span class="built_in">require</span>(<span class="string">'mongoose'</span>);</div><div class="line"><span class="keyword">var</span> Role = mongoose.model(<span class="string">'Role'</span>);</div><div class="line"><span class="keyword">var</span> modelUtil = <span class="built_in">require</span>(<span class="string">'../util/modelUtil.js'</span>);</div><div class="line"></div><div class="line"><span class="comment">/////////////////////////////////////////////////////////////////////</span></div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * [getByUidNoCreate 获取玩家信息，不存在该玩家时不创建对应表]</div><div class="line"> * @param  &#123;[type]&#125;   uid [玩家ID]</div><div class="line"> * @param  &#123;Function&#125; cb  [回调]</div><div class="line"> * @return &#123;[type]&#125;       [description]</div><div class="line"> */</div><div class="line"><span class="built_in">module</span>.exports.getByUidNoCreate = <span class="function"><span class="keyword">function</span>(<span class="params">uid, cb</span>) </span>&#123;</div><div class="line">	Role.findOne(&#123;</div><div class="line">		<span class="attr">uid</span>: uid</div><div class="line">	&#125;, cb);</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * [getByUid 获取玩家信息，不存在该玩家时创建对应表]</div><div class="line"> * @param  &#123;[type]&#125;   uid [玩家ID]</div><div class="line"> * @param  &#123;Function&#125; cb  [回调]</div><div class="line"> * @return &#123;[type]&#125;       [description]</div><div class="line"> */</div><div class="line"><span class="built_in">module</span>.exports.getByUid = <span class="function"><span class="keyword">function</span>(<span class="params">uid, cb</span>) </span>&#123;</div><div class="line">	modelUtil.getByUid(Role, uid, <span class="function"><span class="keyword">function</span>(<span class="params">err, role_model</span>) </span>&#123;</div><div class="line">		<span class="keyword">if</span> (!!err) &#123;</div><div class="line">			<span class="built_in">console</span>.error(err);</div><div class="line">			<span class="keyword">return</span> cb(err);</div><div class="line">		&#125;</div><div class="line"></div><div class="line">		cb(<span class="literal">null</span>, role_model);</div><div class="line">	&#125;);</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<p>注意代码：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> roleSchema = <span class="built_in">require</span>(<span class="string">'../schemas/roleSchema'</span>);</div><div class="line"><span class="keyword">var</span> mongoose = <span class="built_in">require</span>(<span class="string">'mongoose'</span>);</div><div class="line"><span class="keyword">var</span> Role = mongoose.model(<span class="string">'Role'</span>);</div></pre></td></tr></table></figure></p>
<p>这三行用来和 <code>roleSchema</code> 相关联，是必不可少的。</p>
<p>然后我们封装了 <code>modelUtil.js</code> 工具类，当没有查找到对应ID的表的时候，直接创建表并返回。在 <code>roleModel</code> 里面定义了函数 <code>getByUid</code> 和 <code>getByUidNoCreate</code>，用来获取对应 <code>uid</code> 的用户数据。 </p>
<h3 id="创建获取用户信息协议"><a href="#创建获取用户信息协议" class="headerlink" title="创建获取用户信息协议"></a>创建获取用户信息协议</h3><p>完成表的创建后，我们需要通过协议对表进行操作以验证逻辑的正确性。</p>
<h4 id="新建用户服务器"><a href="#新建用户服务器" class="headerlink" title="新建用户服务器"></a>新建用户服务器</h4><p>我们可以新建一个用户服务器用来处理和用户相关的操作。</p>
<p>修改 <code>game-server/config/adminServer.js</code>，新增<code>user</code>的配置：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[&#123;</div><div class="line">	<span class="string">"type"</span>: <span class="string">"connector"</span>,</div><div class="line">	<span class="string">"token"</span>: <span class="string">"agarxhqb98rpajloaxn34ga8xrunpagkjwlaw3ruxnpaagl29w4rxn"</span></div><div class="line">&#125;, &#123;</div><div class="line">	<span class="string">"type"</span>: <span class="string">"user"</span>,</div><div class="line">	<span class="string">"token"</span>: <span class="string">"agarxhqb98rpajloaxn34ga8xrunpagkjwlaw3ruxnpaagl29w4rxn"</span></div><div class="line">&#125;]</div></pre></td></tr></table></figure></p>
<p>修改 <code>game-server/config/development_local/server.json</code>，新增<code>user</code>服务器的配置：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line">	<span class="string">"connector"</span>: [&#123;</div><div class="line">		<span class="string">"id"</span>: <span class="string">"connector-server-1"</span>,</div><div class="line">		<span class="string">"host"</span>: <span class="string">"127.0.0.1"</span>,</div><div class="line">		<span class="string">"port"</span>: <span class="number">4010</span>,</div><div class="line">		<span class="string">"clientPort"</span>: <span class="number">3010</span>,</div><div class="line">		<span class="string">"frontend"</span>: <span class="literal">true</span></div><div class="line">	&#125;],</div><div class="line">	<span class="string">"user"</span>: [&#123;</div><div class="line">		<span class="string">"id"</span>: <span class="string">"user-server-1"</span>,</div><div class="line">		<span class="string">"host"</span>: <span class="string">"127.0.0.1"</span>,</div><div class="line">		<span class="string">"port"</span>: <span class="number">4011</span></div><div class="line">	&#125;]</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h4 id="服务端创建协议"><a href="#服务端创建协议" class="headerlink" title="服务端创建协议"></a>服务端创建协议</h4><p>新建目录和代码文件 <code>game-server/app/server/user/handler/roleHandler.js</code>：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> co = <span class="built_in">require</span>(<span class="string">'co'</span>);</div><div class="line"><span class="keyword">var</span> thunkify = <span class="built_in">require</span>(<span class="string">'thunkify'</span>);</div><div class="line"><span class="keyword">var</span> roleModel = <span class="built_in">require</span>(<span class="string">'../../../models/roleModel.js'</span>);</div><div class="line"></div><div class="line"><span class="comment">// 错误码定义文件</span></div><div class="line"><span class="keyword">var</span> code = <span class="built_in">require</span>(<span class="string">'../../../consts/code.js'</span>);</div><div class="line"></div><div class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="keyword">function</span>(<span class="params">app</span>) </span>&#123;</div><div class="line">	<span class="keyword">return</span> <span class="keyword">new</span> Handler(app);</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="keyword">var</span> Handler = <span class="function"><span class="keyword">function</span>(<span class="params">app</span>) </span>&#123;</div><div class="line">	<span class="keyword">this</span>.app = app;</div><div class="line">&#125;;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 获取玩家信息协议</div><div class="line"> * @return &#123;[type]&#125; [description]</div><div class="line"> */</div><div class="line">Handler.prototype.getInfo = <span class="function"><span class="keyword">function</span>(<span class="params">msg, session, next</span>) </span>&#123;</div><div class="line">	<span class="comment">// 获取客户端传上来的UID </span></div><div class="line">	<span class="keyword">var</span> uid = msg.uid;</div><div class="line">	<span class="keyword">if</span> (!uid) &#123;</div><div class="line">		<span class="keyword">return</span> next(<span class="literal">null</span>, &#123;</div><div class="line">			<span class="attr">code</span>: code.PARAM_ERROR</div><div class="line">		&#125;);</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	<span class="keyword">var</span> onDo = <span class="function"><span class="keyword">function</span>*(<span class="params"></span>) </span>&#123;</div><div class="line">		<span class="comment">// 获取对应Uid的model</span></div><div class="line">		<span class="keyword">var</span> role_model = <span class="keyword">yield</span> thunkify(roleModel.getByUid)(uid);</div><div class="line">		<span class="keyword">return</span> next(<span class="literal">null</span>, &#123;</div><div class="line">			<span class="attr">code</span>: code.OK,</div><div class="line">			<span class="attr">result</span>: &#123;</div><div class="line">				<span class="attr">role_info</span>: role_model.toJSON()</div><div class="line">			&#125;</div><div class="line">		&#125;);</div><div class="line">	&#125;;</div><div class="line"></div><div class="line">	<span class="keyword">var</span> onError = <span class="function"><span class="keyword">function</span>(<span class="params">err</span>) </span>&#123;</div><div class="line">		<span class="built_in">console</span>.error(err);</div><div class="line">		<span class="keyword">return</span> next(<span class="literal">null</span>, &#123;</div><div class="line">			<span class="attr">code</span>: code.FAIL</div><div class="line">		&#125;);</div><div class="line">	&#125;;</div><div class="line"></div><div class="line">	co(onDo).catch(onError);</div><div class="line">&#125;;</div></pre></td></tr></table></figure></p>
<p><code>co</code> 和 <code>thunkify</code> 模块用来帮助实现 Node.js 的异步操作。关于它们的用法可以谷歌一下。</p>
<p>我们在协议中通过客户端传上来的 <code>uid</code> 获取对应的表，然后以 <code>json</code> 格式返回给客户端这个表的数据内容。 </p>
<h4 id="客户端调用协议获取信息"><a href="#客户端调用协议获取信息" class="headerlink" title="客户端调用协议获取信息"></a>客户端调用协议获取信息</h4><p>服务端新建协议后，我们就可以在客户端进行调用。</p>
<p>新建脚本 <code>UserRoleRequest.cs</code> 实现客户端协议的调用：<br><figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 获取用户信息协议</span></div><div class="line"><span class="keyword">public</span> <span class="keyword">class</span> UserRoleRequest : BaseRequest</div><div class="line">&#123;</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getInfo</span><span class="params">()</span></span></div><div class="line">	&#123;</div><div class="line">		<span class="comment">// 定义我的Uid</span></div><div class="line">		<span class="keyword">int</span> my_uid = <span class="number">10000</span>;</div><div class="line">		JsonObject msg = <span class="keyword">new</span> JsonObject();</div><div class="line">		msg.Add(<span class="string">"uid"</span>, my_uid);</div><div class="line">		<span class="keyword">this</span>.Request(ProtocolRoute.USER_ROLE_GETINFO , msg);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>可以看到，里面我们定义了与服务端进行交互的 <code>uid</code>，一般游戏里面这个 <code>uid</code>是根据第一无二的设备号进行处理后得出的，或者接入第三方的账号，这里为了方便，我们直接在本地定义 <code>uid</code> 。 </p>
<p>同时，新建 <code>DataPool</code> 用于在游戏过程中数据的存储。</p>
<p>一般情况下，一登陆游戏，就需要得到用户的信息，而不是等跳转场景后再调用获取对应信息的协议，是故我们创建 <code>LoginData</code> 脚本用于游戏数据的初始化。在脚本中定义获取信息的协议，等协议全部成功后，再进行场景的跳转。 </p>
<p>修改 <code>UILoginWindow</code> 脚本内容：<br><figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">OnEntry</span> <span class="params">(RequestData data)</span></span></div><div class="line">&#123;</div><div class="line">	Debug.Log (<span class="string">"OnEntry"</span>);</div><div class="line">	Debug.Log (data);</div><div class="line"></div><div class="line">	<span class="comment">// 数据池清空</span></div><div class="line">	DataPool.Destroy();</div><div class="line"></div><div class="line">	<span class="comment">// 获取相关登陆数据</span></div><div class="line">	LoginData loginData = <span class="keyword">new</span> LoginData();</div><div class="line">	loginData.onSuccess = <span class="keyword">this</span>.OnLoginDataSuccess;</div><div class="line">	loginData.onError = <span class="keyword">this</span>.OnLoginDataError;</div><div class="line">	loginData.Receive();</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 登陆数据获取成功</span></div><div class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">OnLoginDataSuccess</span><span class="params">()</span></span></div><div class="line">&#123;</div><div class="line">	Loom.DispatchToMainThread(() =&gt; SceneManager.LoadScene(<span class="string">"Main"</span>));</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 登陆数据获取失败</span></div><div class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">OnLoginDataError</span><span class="params">(RequestData data)</span></span></div><div class="line">&#123;</div><div class="line">	<span class="keyword">this</span>.loginBtn.gameObject.SetActive(<span class="literal">true</span>);</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>完成后，点击登陆按钮可以看到服务端返回回来的数据，并且跳转到 <code>Main</code> 场景。 我们可以通过 Robomongo 连接数据库，也可以看到这时候数据库已经不为空，并且存在对应的用户的数据表 <code>roles</code>。</p>
<p>完成这个协议后，你可以尝试下编写一个资源增加的协议，比如说每次客户端调用协议，都会给该用户增加100金币，我们也将会在后续用到这个协议。</p>
<h3 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h3><ul>
<li>mongoose 基本用法 <a href="http://www.jianshu.com/p/9b20c1e2f373" target="_blank" rel="external">http://www.jianshu.com/p/9b20c1e2f373</a></li>
<li>co thunkify 用法<a href="https://gist.github.com/lleo/9928733" target="_blank" rel="external">https://gist.github.com/lleo/9928733</a></li>
<li>彻底理解 thunk 函数与 co 框架 <a href="http://web.jobbole.com/85901/" target="_blank" rel="external">http://web.jobbole.com/85901/</a></li>
</ul>

      
    </div>
    
    <div>
      
        
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script type="text/javascript" src="http://jslibs.wuxubj.cn/sweetalert_mini/jquery-1.7.1.min.js"></script>
  <script src="http://jslibs.wuxubj.cn/sweetalert_mini/sweetalert.min.js"></script>
  <link rel="stylesheet" type="text/css" href="http://jslibs.wuxubj.cn/sweetalert_mini/sweetalert.mini.css">

  <p><span>本文标题:</span><a href="/posts/dc1a186b/">Pomelo教程(7)-新建用户服务器和用户信息表</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 Vitah 的个人博客">Vitah</a></p>
  <p><span>发布时间:</span>2017年03月05日 - 18:03</p>
  <p><span>最后更新:</span>2017年03月05日 - 18:03</p>
  <p><span>原始链接:</span><a href="/posts/dc1a186b/" title="Pomelo教程(7)-新建用户服务器和用户信息表">http://www.vitah.net/posts/dc1a186b/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://www.vitah.net/posts/dc1a186b/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
</div>

<script> 
    var clipboard = new Clipboard('.fa-clipboard');
	clipboard.on('success', $(function(){
	  $(".fa-clipboard").click(function(){
		swal({   
		  title: "",   
		  text: '复制成功',   
		  html: false,
		  timer: 500,   
		  showConfirmButton: false
	    });
	  });
    }));  
</script>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div></div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="/images/wechatpay.jpeg" alt="Vitah WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="/images/alipay.jpeg" alt="Vitah Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Node-js/" rel="tag"># Node.js</a>
          
            <a href="/tags/Pomelo/" rel="tag"># Pomelo</a>
          
            <a href="/tags/Unity3D/" rel="tag"># Unity3D</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/c437b023/" rel="next" title="Centos7下安装MongoDB">
                <i class="fa fa-chevron-left"></i> Centos7下安装MongoDB
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/29a11fc/" rel="prev" title="Centos7安装gcc">
                Centos7安装gcc <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/head.JPG"
               alt="Vitah" />
          <p class="site-author-name" itemprop="name">Vitah</p>
           
              <p class="site-description motion-element" itemprop="description">深自缄默，如云漂泊。</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">47</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">14</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/vitahlin" target="_blank" title="Github">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  Github
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="mailto:linw1225@163.com" target="_blank" title="Mail">
                  
                    <i class="fa fa-fw fa-envelope"></i>
                  
                  Mail
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-block">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              友情链接
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="https://shuge.org/" title="书格" target="_blank">书格</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://taozj.org/" title="taozj" target="_blank">taozj</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://oserror.com/" title="Charles" target="_blank">Charles</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.hsmouc.com/" title="Hsmouc" target="_blank">Hsmouc</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建-roleSchema-js"><span class="nav-number">1.</span> <span class="nav-text">创建 roleSchema.js</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建-roleModel-js"><span class="nav-number">2.</span> <span class="nav-text">创建 roleModel.js</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建获取用户信息协议"><span class="nav-number">3.</span> <span class="nav-text">创建获取用户信息协议</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#新建用户服务器"><span class="nav-number">3.1.</span> <span class="nav-text">新建用户服务器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#服务端创建协议"><span class="nav-number">3.2.</span> <span class="nav-text">服务端创建协议</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#客户端调用协议获取信息"><span class="nav-number">3.3.</span> <span class="nav-text">客户端调用协议获取信息</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#参考链接"><span class="nav-number">4.</span> <span class="nav-text">参考链接</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Vitah</span>
</div>



        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      访客
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>


        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  








  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  


  

    
      <script id="dsq-count-scr" src="https://http-www-vitah-net.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://www.vitah.net/posts/dc1a186b/';
          this.page.identifier = 'posts/dc1a186b/';
          this.page.title = 'Pomelo教程(7)-新建用户服务器和用户信息表';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://http-www-vitah-net.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  





  





  






  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
      search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.popup').toggle();
    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';
      $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = $( "entry", xmlResponse ).map(function() {
            return {
              title: $( "title", this ).text(),
              content: $("content",this).text(),
              url: $( "url" , this).text()
            };
          }).get();
          var $input = document.getElementById(search_id);
          var $resultContent = document.getElementById(content_id);
          $input.addEventListener('input', function(){
            var matchcounts = 0;
            var str='<ul class=\"search-result-list\">';
            var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
            $resultContent.innerHTML = "";
            if (this.value.trim().length > 1) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var content_index = [];
                var data_title = data.title.trim().toLowerCase();
                var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                var data_url = decodeURIComponent(data.url);
                var index_title = -1;
                var index_content = -1;
                var first_occur = -1;
                // only match artiles with not empty titles and contents
                if(data_title != '') {
                  keywords.forEach(function(keyword, i) {
                    index_title = data_title.indexOf(keyword);
                    index_content = data_content.indexOf(keyword);
                    if( index_title >= 0 || index_content >= 0 ){
                      isMatch = true;
                      if (i == 0) {
                        first_occur = index_content;
                      }
                    }

                  });
                }
                // show search results
                if (isMatch) {
                  matchcounts += 1;
                  str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                  var content = data.content.trim().replace(/<[^>]+>/g,"");
                  if (first_occur >= 0) {
                    // cut out 100 characters
                    var start = first_occur - 20;
                    var end = first_occur + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if(start == 0){
                      end = 50;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    var match_content = content.substring(start, end);
                    // highlight all keywords
                    keywords.forEach(function(keyword){
                      var regS = new RegExp(keyword, "gi");
                      match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                    });

                    str += "<p class=\"search-result\">" + match_content +"...</p>"
                  }
                  str += "</li>";
                }
              })};
            str += "</ul>";
            if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
            if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
            $resultContent.innerHTML = str;
          });
          proceedsearch();
        }
      });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>





  

  

  

  

  

</body>
</html>
